﻿using SvgApplication.Authorization;
using SvgApplication.Data.Implementation;
using SvgApplication.Data.Model;
using SvgApplication.Data.Model.Interfaces;
using SvgApplication.Models;
using SvgApplication.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SvgApplication.Controllers
{
    public class AccountController : AuthorizationControllerBase
    {
        IUserRepository userRepository;

        public AccountController()
            : this(new UserRepository())
        {

        }

        public AccountController(IUserRepository userRepository)
        {
            this.userRepository = userRepository;
        }

        //
        // GET: /Account/
        public ActionResult Index()
        {

            return View();
        }

        [HttpGet]
        public ActionResult Login()
        {

            return View();
        }

        [HttpPost]
        public ActionResult Login(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var user = userRepository.Login(model.Username, model.Password);

                    if (user != null)
                    {
                        if (user.Role == (byte)UserRoles.Admin)
                        {
                            base.LoginUser(user.Id, user.Username, "token");
                            WebUserAccess.SetUserAccess(user.Id, (UserRoles)user.Role);
                            return RedirectToAction("Index", "User");
                        }
                        else
                        {
                            if (user.Blocked == false)
                            {
                                base.LoginUser(user.Id, user.Username, "token");
                                WebUserAccess.SetUserAccess(user.Id, (UserRoles)user.Role);
                                return RedirectToAction("Index", "Details");
                            }
                            else
                            {
                                AddError(string.Format("the user is blocked. Reason: {0}", user.BlockReason));
                            }
                        }
                    }
                    else
                    {
                        AddError("invalid username/password");
                    }
                }
                catch (Exception ex)
                {
                    AddError(ex.Message);
                }
            }
            return View(model);
        }

        [HttpGet]
        public ActionResult Register()
        {

            return View();
        }

        [HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    userRepository.Create(new Data.Model.User()
                     {
                         Username = model.Username,
                         Password = model.Password,
                         Role = (byte)UserRoles.RegularUser,
                         Blocked = false
                     });
                    return RedirectToAction("Index", "Home");

                }
                catch (Exception ex)
                {
                    AddError(ex.Message);
                }
            }
            return View(model);
        }

        [HttpGet]
        public ActionResult Welcome()
        {

            return View();
        }

        private void AddError(string message)
        {
            ViewBag.Error = message;
        }

    }
}
